package com.shaco.heroproject.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.ImageRequest;
import com.shaco.heroproject.R;
import com.shaco.heroproject.volley.VolleySingleton;

import java.util.HashMap;
import java.util.Map;


/**
 * Created by dllo on 15/11/7.
 */
public class EquipmentListAdapter extends RecyclerView.Adapter<EquipmentListAdapter.EquipmentListHolder>{
    private String[] skill;
    private String heroName;
    private VolleySingleton queue;

    public EquipmentListAdapter(Context c) {

//        queue = Volley.newRequestQueue(c);
    }


    @Override
    public EquipmentListHolder onCreateViewHolder(ViewGroup viewGroup, int position) {
        LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
        View v = inflater.inflate(R.layout.activity_equipment_item, null);
        return new EquipmentListHolder(v);
    }

    @Override
    public void onBindViewHolder(EquipmentListHolder holder, int position) {
        Log.i("shaco","+++++++++++"+skill.length);
        if(skill!=null&&skill.length>0){
            holder.equipment_skillTv.setText(skill[position]+"");
            String imgUrl = "http://img.lolbox.duowan.com/abilities/" + heroName + "_" + skill[position] + "_64x64.png";
            Log.i("shaco",imgUrl+"");
            getBitmap(imgUrl, holder.equipment_skillIv);

        }

    }

    @Override
    public int getItemCount() {
        return skill!=null&&skill.length>0?skill.length:0;
    }

    public void addData(String[] skills, String name) {
        skill=skills;
        heroName=name;
        notifyDataSetChanged();
    }


    public class EquipmentListHolder extends RecyclerView.ViewHolder {
        private ImageView equipment_skillIv;
        private TextView equipment_skillTv;
        public EquipmentListHolder(View itemView) {
            super(itemView);
            equipment_skillIv= (ImageView) itemView.findViewById(R.id.equipment_skillIv);
            equipment_skillTv= (TextView) itemView.findViewById(R.id.equipment_skillTv);
        }
    }
    // 解析图片
    public void getBitmap(String url, final ImageView imageView) {
        ImageLoader.ImageListener listener = ImageLoader.getImageListener(imageView, R.drawable.ic_launcher, R.drawable.ic_launcher);
        VolleySingleton.getVolleyIntance().getImageLoader().get(url, listener);
        VolleySingleton.getVolleyIntance().getImage(url, new Response.Listener<Bitmap>() {
            @Override
            public void onResponse(Bitmap response) {
                imageView.setImageBitmap(response);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
//        // TODO Auto-generated method stub
//        queue = VolleySingleton.getVolleyIntance();
//        ImageRequest request = new ImageRequest(url,
//                new Response.Listener<Bitmap>() {
//
//                    @Override
//                    public void onResponse(Bitmap response) {
//                        // TODO Auto-generated method stub
//                        // 返回正确图片的回调方法
//                        imageView.setImageBitmap(response);
//                    }
//
//                }, 64, 64, Bitmap.Config.ARGB_8888, new Response.ErrorListener() {
//
//            @Override
//            public void onErrorResponse(VolleyError error) {
//                // TODO Auto-generated method stub
//                error.printStackTrace();
//            }
//        }) {
//
//            @Override
//            public Map<String, String> getHeaders() throws AuthFailureError {
//                Map<String, String> map = new HashMap<String, String>();
//                // 在请求头中添加apikey
//                map.put("Accept-Encoding", "");
//                return map;
//            }
//
//        };
////        queue.add(request);
//        queue.addRequest(request);
    }
}
